package service

import (
	"animal_srv/basic/appConfig"
	__ "animal_srv/basic/proto"
	"animal_srv/hander/models"
	"context"
	"encoding/json"
	"errors"
	"fmt"
	"github.com/google/uuid"
	"time"
)

// server is used to implement helloworld.GreeterServer.
type Server struct {
	__.UnimplementedAnimalServer
}

// SayHello implements helloworld.GreeterServer
func (s *Server) AddAnimal(_ context.Context, in *__.AddAnimalReq) (*__.AddAnimalResp, error) {
	var err error
	var a models.MyAnimal

	if err = a.GeyAnimalInfoByNickName(appConfig.DB, in.NickName); err != nil {
		return nil, errors.New("宠物信息获取失败！")
	}
	if a.ID != 0 {
		return nil, errors.New("该宠物已添加！")
	}

	a.Image = in.Image
	a.NickName = in.NickName
	a.Age = in.Age
	a.Sex = in.Sex
	a.TypesId = in.TypesId
	a.TheSexy = in.TheSexy
	a.TheHealth = in.TheHealth
	a.TheQu = in.TheQu

	if err = a.Create(appConfig.DB); err != nil {
		return nil, errors.New("宠物添加失败")
	}

	return &__.AddAnimalResp{
		Id: int64(a.ID),
	}, nil
}

func (s *Server) Info(_ context.Context, in *__.InfoReq) (*__.InfoResp, error) {
	var err error
	var t models.TypesList

	if err = t.TypesInfo(appConfig.DB, in.Id); err != nil {
		return nil, errors.New("详情查看失败")
	}

	return &__.InfoResp{
		Title:   t.Title,
		Price:   t.Price,
		Comment: t.Comment,
		Stock:   t.Stock,
	}, nil
}

func (s *Server) TypesList(_ context.Context, in *__.TypesListReq) (*__.TypesListResp, error) {
	var err error
	var ts models.TypesList
	var tss []models.TypesList
	var tsList []*__.ListTypes

	key := fmt.Sprintf("TypesList")
	cacheData, _ := appConfig.Rdb.Get(appConfig.Ctx, key).Result()

	if cacheData == "" {
		tss, err = ts.ListTypes(appConfig.DB)
		if err != nil {
			return nil, err
		}

		jsonData, _ := json.Marshal(tss)
		appConfig.Rdb.Set(appConfig.Ctx, key, jsonData, time.Hour)
	} else {
		err := json.Unmarshal([]byte(cacheData), &tss)
		if err != nil {
			return nil, err
		}
	}

	for _, t := range tss {
		list := __.ListTypes{
			Title:   t.Title,
			Price:   t.Price,
			Comment: t.Comment,
			Stock:   t.Stock,
		}
		tsList = append(tsList, &list)
	}

	return &__.TypesListResp{
		List: tsList,
	}, nil
}

func (s *Server) Change(_ context.Context, in *__.ChangeReq) (*__.ChangeResp, error) {
	var err error
	var c models.ChangeTypes

	c.PopeId = in.PopeId
	c.AdsId = in.AdsId
	c.Shop = in.Shop
	c.TypesId = in.TypesId

	if err = c.CreateChangeTypes(appConfig.DB); err != nil {
		return nil, errors.New("宠物的选择失败")
	}

	return &__.ChangeResp{
		Msg: "宠物的选择成功",
	}, nil
}

func (s *Server) AdsList(_ context.Context, in *__.AdsListReq) (*__.AdsListResp, error) {

	var as models.MyAds
	var asList []*__.ListAds

	ads, err := as.ListMyAds(appConfig.DB, in.UserId)
	if err != nil {
		return nil, errors.New("查询失败")
	}

	for _, a := range ads {
		list := __.ListAds{
			NickName: a.NickName,
			Mobile:   a.Mobile,
			Ads:      a.Ads,
		}
		asList = append(asList, &list)
	}

	return &__.AdsListResp{
		UserId: in.UserId,
		List:   asList,
	}, nil
}

func (s *Server) CreateOrder(_ context.Context, in *__.CreateOrderReq) (*__.CreateOrderResp, error) {

	var err error
	var o models.Orders
	var t models.TypesList

	if in.UserId == 0 || in.GoodsId == 0 || in.TeacherId == 0 {
		return nil, errors.New("必要参数不能为空")
	}

	o.UserId = in.UserId
	o.GoodsID = in.GoodsId
	o.TeacherId = in.TeacherId
	o.DataTime = time.Now()
	o.Comment = in.Comment
	o.Total = t.Price * float32(in.Num)
	o.OrderSn = uuid.NewString()

	if models.GetStockLen(int(in.GoodsId)) < 0 {
		return nil, errors.New("库存不足")
	}

	if err = o.GetOrderByOrderSn(appConfig.DB, o.OrderSn); err != nil {
		return nil, errors.New("订单重复创建")
	}

	if err = o.CreateOrder(appConfig.DB); err != nil {
		return nil, errors.New("订单创建失败")
	}

	return &__.CreateOrderResp{
		OrderSn: o.OrderSn,
	}, nil
}

func (s *Server) SyncStock(_ context.Context, in *__.SyncStockReq) (*__.SyncStockResp, error) {

	models.SyncStock(int(in.GoodsId), int(in.Num))

	return &__.SyncStockResp{
		Msg: "库存设置成功",
	}, nil
}
